const db = require('../../lib/conn_oracle')

exports.getPersonGroupInfo = async function (req, res) {
    let { persongroup,cust_type } = req.body.params
    // console.log(req.body)
    let pgWhere = persongroup.length ? ` and p.persongroup = :persongroup` : ""
    let ctWhere = cust_type.length ? ` and p.cust_type = :cust_type` : ""
    let resMsg = ""
    let resStatus = 1
    let resDatas = {}
    let reqData = []
    if(persongroup.length){
        reqData.push(persongroup)
    }
    if(cust_type.length){
        reqData.push(cust_type)
    }
    let searchSql = `select *
                     from c##wxw.persongroup p
                     where 1=1
                     ${pgWhere} 
                     ${ctWhere}
                     order by p.persongroup
                    `
    try {
        let personGroupInfo = persongroup.length || cust_type.length ? await db.search(searchSql, reqData) : await db.search2(searchSql)
        resMsg = personGroupInfo.errorNum ? "查询失败" : "查询成功"
        resStatus = personGroupInfo.errorNum ? 0 : 1
        resDatas = { personGroupInfo }
        res.json({
            resMsg,
            status: resStatus,
            resDatas
        })
    } catch (err) {
        res.json({
            msg: "查询失败" + err,
            status: 0
        })
    }
}

exports.editPersonGroup = async function (req, res) {
    console.log(req.body)
    let { type, data } = req.body
    let { LINE,JURISDICTION,GONGBAN,TEL_PG,SPECNUMS,PGNAME,PERSONGROUP,LINENUM,CUST_TYPE } = data
    let resMsg = ""
    let msgType = ""
    let resStatus = 1
    let resDatas = {}
    let editDatas = []
    let editSql = ""

    if(type === "add" || type === "copy"){
        msgType = "新增"
        editDatas = [ LINE,JURISDICTION,GONGBAN,TEL_PG,SPECNUMS,PGNAME,PERSONGROUP,LINENUM,CUST_TYPE ]
        editSql = `insert into c##wxw.persongroup(LINE,JURISDICTION,GONGBAN,TEL_PG,SPECNUMS,PGNAME,PERSONGROUP,LINENUM,CUST_TYPE)
                   values(:LINE,:JURISDICTION,:GONGBAN,:TEL_PG,:SPECNUMS,:PGNAME,:PERSONGROUP,:LINENUM,:CUST_TYPE)`
        let sql = `select * from c##wxw.persongroup where persongroup = :PERSONGROUP`
        let personGroupInfo = await db.search(sql,[PERSONGROUP])
        if(personGroupInfo.length){
            res.json({
                msg:`新增失败，人员组${PERSONGROUP}已存在`,
                status:0
            })
            return
        }
    }
    if(type === "update"){
        msgType = "更新"
        editDatas = [ LINE,JURISDICTION,GONGBAN,TEL_PG,SPECNUMS,PGNAME,LINENUM,CUST_TYPE,PERSONGROUP ]
        editSql = `update c##wxw.persongroup 
                    set LINE=:LINE,
                    JURISDICTION=:JURISDICTION,
                    GONGBAN=:GONGBAN,
                    TEL_PG=:TEL_PG,
                    SPECNUMS=:SPECNUMS,
                    PGNAME=:PGNAME,
                    LINENUM=:LINENUM,
                    CUST_TYPE=:CUST_TYPE 
                    where PERSONGROUP=:PERSONGROUP`
    }
    if(type === "delete"){
        msgType = "删除"
        editDatas = [ PERSONGROUP ]
        editSql = `delete from c##wxw.persongroup where PERSONGROUP=:PERSONGROUP`
    }

    try{
        let newPersongroup = await db.writeTab(editSql,editDatas)
        resMsg = newPersongroup.errorNum ? `${msgType}失败` : `${msgType}成功`
        resStatus = newPersongroup.errorNum ? 0 : 1
        resDatas = { newPersongroup }
        res.json({
            msg:resMsg,
            status: resStatus,
            resDatas
        })
    }catch(err){
        res.json({
            msg:`${msgType}失败`+err,
            status:0
        })
    }
}

exports.batchEditPersonGroup = async function (req, res) {
    // console.log(req.body)
    let { type, data } = req.body
    let resMsg = ""
    let msgType = ""
    let resStatus = 1
    let resDatas = {}
    let editDatas = []
    let editSql = ""
    let options = {
        autoCommit: true
    }
    if(type === "batchAdd"){
        msgType = "批量新增"
        editDatas = data
        editSql = `insert into c##wxw.persongroup(LINE,JURISDICTION,GONGBAN,TEL_PG,SPECNUMS,PGNAME,PERSONGROUP,LINENUM,CUST_TYPE)
                   values(:LINE,:JURISDICTION,:GONGBAN,:TEL_PG,:SPECNUMS,:PGNAME,:PERSONGROUP,:LINENUM,:CUST_TYPE)`
        let needAddPgs = []
        data.forEach(item => {
            item.PERSONGROUP && needAddPgs.push(item.PERSONGROUP)
        });
        // console.log(needAddPgs)
        let searchSql = `select p.persongroup
                         from c##wxw.persongroup p
                         where p.persongroup in ('${needAddPgs.join("','")}')
                        `
        // console.log(searchSql)
        let personGroupInfo = await db.search2(searchSql)
        // console.log(personGroupInfo)
        let personGroupInfoList = personGroupInfo.map(item => item.PERSONGROUP)
        let diff = !personGroupInfoList.length ? [] : needAddPgs.filter(item => personGroupInfoList.includes(item))
        // console.log(diff)
        if(diff.length){
            res.json({
                msg:`${msgType}失败，存在重复的人员组：${diff.join(",")}`,
                status:0
            })
            return
        }
    }
    if(type === "batchUpdate"){
        msgType = "批量更新"
        editDatas = data
        editSql = `update c##wxw.persongroup 
                    set LINE=:LINE,
                    JURISDICTION=:JURISDICTION,
                    GONGBAN=:GONGBAN,
                    TEL_PG=:TEL_PG,
                    SPECNUMS=:SPECNUMS,
                    PGNAME=:PGNAME,
                    LINENUM=:LINENUM,
                    CUST_TYPE=:CUST_TYPE 
                    where PERSONGROUP=:PERSONGROUP`
    }
    if(type === "batchDel"){
        msgType = "批量删除"
        editDatas = data
        editSql = `delete from c##wxw.persongroup where PERSONGROUP=:PERSONGROUP`
    }

    try{
        let newPersongroup = await db.writeManyTab(editSql,editDatas,options)
        resMsg = newPersongroup.errorNum ? `${msgType}失败` : `${msgType}成功`
        resStatus = newPersongroup.errorNum ? 0 : 1
        resDatas = { newPersongroup }
        res.json({
            msg:resMsg,
            status: resStatus,
            resDatas
        })
    }catch(err){
        res.json({
            msg:`${msgType}失败`+err,
            status:0
        })
    }
}